package summary;

import java.util.Arrays;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/4 16:14
 * https://leetcode.cn/problems/next-permutation/
 */
public class Title31 {


    public static void nextPermutation(int[] nums) {
        for (int i = nums.length - 2; i >= 0; i--) {
            for (int j = nums.length - 1; j > i; j--) {
                //从右往左找一个
                if (nums[i] < nums[j]) {
                    swap(nums, i, j);
                    //交换后，排序 j 之后
                    Arrays.sort(nums, j + 1, nums.length);
                    return;
                }
            }
        }
        Arrays.sort(nums);
    }

    private static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }


    public static void main(String[] args) {
        int[] arr = {3,2,1};
        nextPermutation(arr);
    }
}
